/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FeedPanel.java
 *
 * Created on 10/07/2010, 20:43:19
 */
package net.jcdev.coolrss.view;

import com.explodingpixels.macwidgets.IAppWidgetFactory;
import com.explodingpixels.macwidgets.MacWidgetFactory;
import com.explodingpixels.macwidgets.plaf.IAppScrollBarUI;
import com.explodingpixels.widgets.TableUtils;
import com.sun.syndication.feed.synd.SyndEntryImpl;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.event.ListSelectionEvent;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import net.jcdev.coolrss.model.entity.Entry;
import net.jcdev.coolrss.model.entity.Feed;
import thinlet.swing.container.SplitPane;
import thinlet.swing.table.Cell;
import thinlet.swing.table.Table;

/**
 *
 * @author carlos
 */
public class FeedPanel extends javax.swing.JPanel implements Table.Listener<Item>  {

    private JPanel cards;
    private Table<Item> feedList;

    private Feed feed;

    /** Creates new form FeedPanel */
    public FeedPanel() {
        initComponents();

        cards = new JPanel(new CardLayout());

        this.add(cards, BorderLayout.CENTER);

        feedList = new Table<Item>();
        feedList.setCells(Item.class,
                new Cell("title").setColor(Color.blue).newLine(),
                new Cell("publishDate").newLine().setColor(Color.darkGray).setFont(UIManager.getFont("Label.smallfont")).setDateFormat(),
                new Cell("author").newLine());
        feedList.addListListener(this);
        //JScrollPane scrollPane = IAppWidgetFactory.createScrollPaneWithButtonsTogether(feedList);
        JScrollPane scrollPane = new JScrollPane(feedList);
        //scrollPane = IAppWidgetFactory.makeIAppScrollPane(scrollPane);
        scrollPane.setBorder(BorderFactory.createEmptyBorder());

        cards.add(scrollPane, "0");



    }

    public JScrollPane createEntryView(Entry entry) {

        JPanel panel = new JPanel(new BorderLayout());

        JEditorPane descriptionPane = new JEditorPane();
        JPanel headerPanel = new JPanel(new BorderLayout());

        headerPanel.add(new JLabel("<html><h3>" + entry.getEntryTitle() + "</h3></html>"));

        descriptionPane.setContentType("text/html");
        descriptionPane.setEditable(false);
        descriptionPane.setText(entry.getEntryDescription());

        panel.add(headerPanel, BorderLayout.NORTH);
        panel.add(descriptionPane, BorderLayout.CENTER);

        JScrollPane scrollPane = new JScrollPane(panel);

        return scrollPane;
    }

    public void populateTable(Feed feed) {

        this.feed = feed;

        List<Entry> entries = this.feed.getEntryList();

        for (Entry e : entries) {
            Item item = new Item();
            item.setTitle(e.getEntryTitle());
            item.setPublishDate(e.getEntryPublishedDate());
            item.setAuthor(e.getEntryAuthor());
            feedList.addItem(item);
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setLayout(new java.awt.BorderLayout());
    }// </editor-fold>//GEN-END:initComponents

    @Override
    public void selectionChanged(ListSelectionEvent e) {
        List<Entry> entries = this.feed.getEntryList();
        Entry entry = entries.get(e.getLastIndex());
        cards.add(this.createEntryView(entry), "1");
        CardLayout layout = (CardLayout) cards.getLayout();
        layout.next(cards);
        //layout.show(this.createEntryView(entry), "1");
    }

    @Override
    public void doubleClicked(Item item) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}

